package com.lk.controller;

import com.github.pagehelper.util.StringUtil;
import com.lk.bean.Admin;
import com.lk.bean.Student;
import com.lk.bean.Teacher;
import com.lk.request.LoginRequest;
import com.lk.service.AdminService;
import com.lk.service.StudentService;
import com.lk.service.TeacherService;
import com.lk.util.CreateVerifiCodeImage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/system")
public class SystemController {

    @Resource
    private StudentService studentService;

    @Resource
    private AdminService adminService;

    @Resource
    private TeacherService teacherService;


    /**
     * 调用验证码工具类生成验证码
     *
     * @param request
     * @param response
     */
    @GetMapping("/getVerifiCodeImage")
    public void getVerifiCodeImage(HttpServletRequest request, HttpServletResponse response) {

        //生成验证码图片
        BufferedImage verifiCodeImage = CreateVerifiCodeImage.getVerifiCodeImage();

        //通过验证码工具类生成验证码
        String verifiCode = String.valueOf(CreateVerifiCodeImage.getVerifiCode());

        //验证码存储到session
        HttpSession session = request.getSession();
        session.setAttribute("verifiCode", verifiCode);

        //把验证码图片生成到登录页面
        try {
            ImageIO.write(verifiCodeImage, "PNG", response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 登录成功，跳转到主页面
     *
     * @return
     */
    @GetMapping("/goSystemMainView")
    public String goSystemMainView() {
        //跳转到main.jsp
        return "system/main";
    }


    /**
     * 登录
     *
     * @param loginRequest
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(LoginRequest loginRequest, HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<>();

        //获取用户输入的验证码
        String inputVerifiCode = loginRequest.getVerifiCode();
        if (StringUtil.isEmpty(inputVerifiCode)) {
            result.put("success", false);
            result.put("msg", "未输入验证码");
            return result;
        }
        //从session中获取验证码
        String verifiCode = request.getSession().getAttribute("verifiCode").toString();
        if (!inputVerifiCode.equalsIgnoreCase(verifiCode)) {
            result.put("success", false);
            result.put("msg", "验证码输入错误");
            return result;
        }
        //验证码校验通过
        request.getSession().removeAttribute("verifiCode");

        //校验账号和密码
        switch (loginRequest.getUserType()) {
            case "1":
                //管理员
                Admin admin = adminService.login(loginRequest);
                if (admin != null) {
                    request.getSession().setAttribute("userInfo", admin);
                    request.getSession().setAttribute("userType", loginRequest.getUserType());
                    result.put("success", true);
                    return result;
                }
                result.put("success", false);
                result.put("msg", "登录失败");
                return result;
//            break;

            case "2":
                //学生
                Student student = studentService.login(loginRequest);
                if (student != null) {
                    request.getSession().setAttribute("userInfo", student);
                    request.getSession().setAttribute("userType", loginRequest.getUserType());
                    result.put("success", true);
                    return result;
                }
                result.put("success", false);
                result.put("msg", "登录失败");
                return result;
//            break;

            case "3":
                //管理员
                Teacher teacher = teacherService.login(loginRequest);
                if (teacher != null) {
                    request.getSession().setAttribute("userInfo", teacher);
                    request.getSession().setAttribute("userType", loginRequest.getUserType());
                    result.put("success", true);
                    return result;
                }
                result.put("success", false);
                result.put("msg", "登录失败");
                return result;
//            break;

        }
        return result;
    }


    /**
     * 退出登录
     *
     * @param session
     */
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session) {
        session.removeAttribute("userInfo");
        session.removeAttribute("userType");
        return "system/login";
    }


}
